/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ece.fr.edu.fac.controleur.oad;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;
import java.sql.Statement;



/**
 *
 * @author smarzoug
 */
public class DAOAbstraite {
    
    public static  Connection initConnection()
	{
    
    Connection conn = null;
		if (conn==null)
		{
			try
			{
				 try {
					Class.forName("com.mysql.jdbc.Driver").newInstance();
				} catch (InstantiationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				 String url =("jdbc:mysql://localhost:3306/Edu-Fac");
				 try {
					conn =DriverManager.getConnection(url,"root", "rbThbXcnjKmb8L5j");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				conn.setAutoCommit(true);

			}
			catch (SQLException e)
			{
				System.out.println("Exception in retreiving the database connection: " + e.getMessage());
			}
		}
		return conn;
	}

	public  static void releaseConnection(Connection con, ResultSet rst , PreparedStatement pst, Statement st)
	{
		 try {
			  if(st != null) { 
				  st.close();
			  }
			  
			  if(pst != null) {
				  pst.close();
			  }
			  
			  if(rst != null) { 
				  rst.close();
			  }
			  
			  if( con != null) { 
				  con.close();
			  }
		 }
		 catch (Exception e) {
			// TODO: handle exception
			 e.printStackTrace();
		}
	}
    
}
